/* This file is part of MyRename
 * Copyright (C) 2005-2009 Kai-Chieh Ku (kjackie@gmail.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef _MYRENAME_OTHERS_H_
#define _MYRENAME_OTHERS_H_
#pragma once

#define VER 0

size_t strneq(LPCTSTR s1, LPCTSTR s2);
size_t strndigit(LPCTSTR str);
int strdigitcmp(LPCTSTR s1, LPCTSTR s2);
int smartstrcmp(LPCTSTR s1, LPCTSTR s2);	//smart string compare (case sensitive)
int smartstricmp(LPCTSTR s1, LPCTSTR s2);	//smart string compare (case insensitive)
int sstricmp(LPCTSTR s1, LPCTSTR s2);		//normal string compare but it is safe with NULL string (case insensitive)
size_t sstrlen(LPCTSTR str);				//get string length and return 0 if 'str' is NULL

LPTSTR tmpdir(LPCTSTR path, LPTSTR dir);

int ReplaceNoCase(CString &dest, LPCTSTR pat, LPCTSTR rp);

bool isInteger(LPCTSTR str);

LPTSTR strToLower(LPTSTR dest, LPCTSTR src);
LPTSTR strToUpper(LPTSTR dest, LPCTSTR src);

LPTSTR strnqcpy(LPTSTR dest, LPCTSTR src, size_t num);

int GetFileIcon(LPCTSTR filepath);

//void Big5Fix(LPWSTR str);
void FullToHalf(LPWSTR str);

inline bool IsCtrlPressed()
{
	return (GetKeyState(VK_CONTROL) & 0x8000) != 0;
}

inline bool IsShiftPressed()
{
	return (GetKeyState(VK_SHIFT) & 0x8000) != 0;
}

inline bool IsAltPressed()
{
	return (GetKeyState(VK_MENU) & 0x8000) != 0;
}

inline bool IsWinPressed()
{
	return (GetKeyState(VK_LWIN) & 0x8000) != 0 || (GetKeyState(VK_RWIN) & 0x8000) != 0;
}

class LastErrorMsg
{
public:
	LastErrorMsg() : lpErrMsgBuffer(NULL) { FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, (LPTSTR) &lpErrMsgBuffer, 0, NULL); }
	~LastErrorMsg() { if (lpErrMsgBuffer) LocalFree(lpErrMsgBuffer); }
	operator LPCTSTR() {return (LPCTSTR) lpErrMsgBuffer;}
private:
	LPVOID lpErrMsgBuffer;
};

class AutoUnlock
{
public:
	AutoUnlock(bool &bLockerFlag)
	{
		m_pLockerFlag = &bLockerFlag;
		*m_pLockerFlag = true;
	}
	AutoUnlock(bool *pLockerFlag)
	{
		m_pLockerFlag = pLockerFlag;
		*m_pLockerFlag = true;
	}
	~AutoUnlock()
	{
		*m_pLockerFlag = false;
	}
	bool Lock()
	{
		if (*m_pLockerFlag)
			return false;
		*m_pLockerFlag = true;
		return true;
	}
	bool Unlock()
	{
		if (!*m_pLockerFlag)
			return false;
		*m_pLockerFlag = false;
		return true;
	}
	bool IsLocking() const
	{
		return *m_pLockerFlag;
	}
	operator bool() const
	{
		return *m_pLockerFlag;
	}
private:
	bool *m_pLockerFlag;
};

#ifdef _DEBUG
#define MSGIDBG(i) {CString msg;msg.Format(_T("%i"), (i));AfxMessageBox(msg);}
#else
#define MSGIDBG(i)
#endif

#endif //_MYRENAME_OTHERS_H_
